package com.wude.guild;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AddressBean;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

public class AddAddressActivity extends TitleBaseActivity implements OnAddressSelectedListener, View.OnClickListener {
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.shiqu)
    TextView shiqu;
    @BindView(R.id.address)
    EditText address;
    @BindView(R.id.moren)
    CheckBox moren;
    @BindView(R.id.right_text)
    TextView saveButton;
    private BottomDialog dialog;
    private AddressBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);

        bean = (AddressBean) getIntent().getSerializableExtra("key");
        if (bean != null) {
            setHeaderTitle("编辑地址");
            name.setText(bean.name);
            phone.setText(bean.phone);
            shiqu.setText(bean.shiqu);
            address.setText(bean.address);
        } else {
            setHeaderTitle("添加地址");
        }

        TextView rightTextView = getTitleHeaderBar().getRightTextView();
        rightTextView.setText("保存");
        rightTextView.setOnClickListener(this);
        shiqu.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void Save() {
        String name1 = name.getText().toString();
        String phone1 = phone.getText().toString();
        String shiqu1 = shiqu.getText().toString();
        String address1 = address.getText().toString();
        if (name1.equals("")) {
            ToastUtil.showToast("请输入姓名");
            saveButton.setClickable(true);
            return;
        }
        if (phone1.equals("")) {
            ToastUtil.showToast("请输入电话号码");
            saveButton.setClickable(true);
            return;
        }
        if (!Utils.isMobileNO(phone1)) {
            ToastUtil.showToast("电话号码不正确");
            saveButton.setClickable(true);
            return;
        }
        if (shiqu1.equals("请选择")) {
            ToastUtil.showToast("请选择省市区");
            saveButton.setClickable(true);
            return;
        }
        if (address1.equals("")) {
            ToastUtil.showToast("请输入详细地址");
            saveButton.setClickable(true);
            return;
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("name", name1);
        map.put("token", Utils.getLoginUser().token);
        map.put("phone", phone1);
        if (moren.isChecked()) {
            map.put("is_default", 1 + "");      // 设为默认
        } else {
            map.put("is_default", 2 + "");      //不设为默认
        }
        map.put("address", address1);
        map.put("city", shiqu1);
        if (bean != null) {
            map.put("id", bean.id + "");
            HttpCom.POST(handler, HttpCom.getBJAddressURL(), map, false);
        } else {
            HttpCom.POST(handler, HttpCom.getSaveAddressURL(), map, false);
        }
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        String s = (province == null ? "" : province.name) +
                (city == null ? "" : "-" + city.name) +
                (county == null ? "" : "-" + county.name) +
                (street == null ? "" : "-" + street.name);
        shiqu.setText(s);
        dialog.dismiss();
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            saveButton.setClickable(true);
            switch (msg.what) {
                case 1:
                    boolean a = HttpUtils.DNSSaveAddress(msg.obj.toString());
                    if (a) {
                        AddAddressActivity.this.finish();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shiqu:
                dialog = new BottomDialog(this);
                dialog.setOnAddressSelectedListener(this);
                dialog.show();
                break;
            case R.id.right_text:
                saveButton.setClickable(false);
                Save();
                break;
        }
    }
}
